<?php
General::getTpl()->assign('extraTitle',"Seiten");

if(!Admin::checkPermission('canManagePages')) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{

	if(isset($_GET['do']) && $_GET['do'] == "new") {
		// New Page
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Seite erstellen",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// Create Page
			Pages::createPage($_POST['Name']);
			$id = mysql_insert_id();
			Navigation::addPredefinedLink("page_".$id,
				"Startseite (ID: ".$id.", Name: ".$_POST['Name'].")",
				"?page=home&amp;id=".$id,
				"home/".$id."_".Navigation::parseLinkText($_POST['Name'])."/");
			General::ouputMessage('Erfolgreich','Die Seite wurde erfolgreich gespeichert.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "edit" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		// Edit Page?
		// Get Pagename
		$query = DB::getInstance()->query("
			SELECT
				pageName
			FROM
				".Tables::$pageTbl."
			WHERE
				id = '".DB::parseVar($_GET['id'])."'");
		$pageName = DB::getInstance()->fetchArray($query);
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = $pageName['pageName'];
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$form = new Form();
		$form->initByData("Seite bearbeiten",true,"Speichern!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			// edit Page
			Pages::editPage($_GET['id'],$_POST['Name']);
			Navigation::editPredefinedLink("page_".$_GET['id'],
				"Startseite (ID: ".$_GET['id'].", Name: ".$_POST['Name'].")",
				"?page=home&amp;id=".$_GET['id'],
				"home/".$_GET['id']."_".Navigation::parseLinkText($_POST['Name'])."/");
			General::ouputMessage('Erfolgreich','Die Seite wurde erfolgreich bearbeitet.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	if(isset($_GET['do']) && $_GET['do'] == "del" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		require_once './kernel/formCheck.php';
		$form = new FormCheck("Bist du sicher?","L&ouml;schen");
		// Done?
		if($form->completeCheck()) {
			if($form->isChecked()) {
				// Delete navigation
				Pages::deletePage($_GET['id']);
			}
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	
	if(isset($_GET['do']) && $_GET['do'] == "setStandard" && isset($_GET['id']) && is_numeric($_GET['id'])) {
		Settings::getInstance()->changeSetting("pages.defaultPage",$_GET['id']);
	}

	// Get Pages
	General::getTpl()->assign('pages',Pages::getPages());
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/pages.html'));
}
?>